#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int minCost(int n, vector<int>& cuts) {
        cuts.push_back(0);
        cuts.push_back(n);
        sort(cuts.begin(), cuts.end());
        int len = cuts.size();
        vector<vector<long long>> dp(len, vector<long long>(len, INT_MAX));
        for (int i = 0; i < len - 1; i++) dp[i][i + 1] = 0;
        for (int i = len - 2; i >= 0; i--) {
            for (int j = i + 2; j < len; j++) {
                for (int k = i + 1; k < j; k++) {
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
                }
                dp[i][j] += cuts[j] - cuts[i];
            }
        }
        // for(int i = 0; i < len; i++){
        //     for(int j = 0; j < len; j++) cout << dp[i][j] << ' ';
        //     cout << endl;
        // }
        return dp[0][len - 1];
    }
};